<?php
require_once 'header.php';

// 检查是否已登录，如果已登录则重定向到首页
if (isLoggedIn()) {
    header('Location: index.php');
    exit;
}

// 处理注册表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $error = '';
    
    // 验证输入
    if (empty($username) || empty($email) || empty($password) || empty($confirm_password)) {
        $error = '所有字段都是必填的';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = '请输入有效的电子邮件地址';
    } elseif ($password !== $confirm_password) {
        $error = '两次密码输入不一致';
    } elseif (strlen($password) < 6) {
        $error = '密码长度至少为6个字符';
    } else {
        // 检查用户名是否已存在
        $check_username = "SELECT id FROM users WHERE username = ?";
        $stmt = $conn->prepare($check_username);
        $stmt->bind_param("s", $username);
        $stmt->execute();
        $result = $stmt->get_result();
        
        if ($result->num_rows > 0) {
            $error = '用户名已被使用，请选择其他用户名';
        } else {
            // 检查邮箱是否已存在
            $check_email = "SELECT id FROM users WHERE email = ?";
            $stmt = $conn->prepare($check_email);
            $stmt->bind_param("s", $email);
            $stmt->execute();
            $result = $stmt->get_result();
            
            if ($result->num_rows > 0) {
                $error = '该电子邮件地址已被注册';
            } else {
                // 所有验证通过，可以注册用户
                $hashed_password = password_hash($password, PASSWORD_DEFAULT);
                
                $sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
                $stmt = $conn->prepare($sql);
                $stmt->bind_param("sss", $username, $email, $hashed_password);
                
                if ($stmt->execute()) {
                    // 注册成功，自动登录用户
                    $_SESSION['user_id'] = $stmt->insert_id;
                    $_SESSION['username'] = $username;
                    
                    // 设置成功消息
                    $_SESSION['flash_message'] = '注册成功，欢迎加入！';
                    $_SESSION['flash_message_type'] = 'success';
                    
                    // 重定向到首页
                    header('Location: index.php');
                    exit;
                } else {
                    $error = '注册失败，请稍后重试: ' . $stmt->error;
                }
            }
        }
    }
}
?>

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="card my-5">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">用户注册</h4>
                </div>
                <div class="card-body">
                    <?php if (!empty($error)): ?>
                    <div class="alert alert-danger" role="alert">
                        <?php echo $error; ?>
                    </div>
                    <?php endif; ?>
                    
                    <form action="register.php" method="post">
                        <div class="form-group">
                            <label for="username">用户名</label>
                            <input type="text" class="form-control" id="username" name="username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="email">电子邮件</label>
                            <input type="email" class="form-control" id="email" name="email" value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="password">密码</label>
                            <input type="password" class="form-control" id="password" name="password" required>
                            <small class="form-text text-muted">密码长度至少为6个字符</small>
                        </div>
                        <div class="form-group">
                            <label for="confirm_password">确认密码</label>
                            <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                        </div>
                        <div class="form-group form-check">
                            <input type="checkbox" class="form-check-input" id="agree" name="agree" required>
                            <label class="form-check-label" for="agree">我同意网站的服务条款和隐私政策</label>
                        </div>
                        <button type="submit" class="btn btn-primary btn-block">注册</button>
                    </form>
                </div>
                <div class="card-footer text-center">
                    <p class="mb-0">已有账号？<a href="login.php">立即登录</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once 'footer.php'; ?> 